package com.haoshuang.zhouzhoubang.ui.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.haoshuang.zhouzhoubang.databinding.FragmentOrderListBinding
import com.haoshuang.zhouzhoubang.model.bean.NumBean
import com.haoshuang.zhouzhoubang.model.event.OrderStatusChangeEvent
import com.haoshuang.zhouzhoubang.ui.adapter.ViewPagerAdapter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *  已接单订单列表
 */
class OrderListFragment : BaseVMFragment<BaseViewModel, FragmentOrderListBinding>() {

    companion object {
        fun getInstance(status: Int, orderStatus: Int): OrderListFragment {
            val fragment = OrderListFragment()
            fragment.arguments = Bundle().apply {
                putInt("status", status)
                putInt("orderStatus", orderStatus)
            }
            return fragment
        }
    }


    // 1: 待服务, 2: 进行中, 3: 已完成, 4: 退款/售后 5: 返工单
    private var status = 1
    // 详细状态 8: 待确认, 1: 待上门, 7: 已超时, 2: 服务中, 6: 待验收, 3: 已完成, 4: 退款/售后, 9: 待预约 10: 上门中
    private var orderStatus = 1

    private val fragments = arrayListOf<Fragment>()

    override fun initArgument() {
        status = arguments?.getInt("status")?:0
        orderStatus = arguments?.getInt("orderStatus")?:0
    }

    override fun initView(savedInstanceState: Bundle?) {

        // 待预约
        binding.includeAppointment.tvStatus.text = "待预约"
        // 待确认
        binding.includeUnconfirmed.tvStatus.text = "待确认"
        // 待上门
        binding.includeUnDoorIn.tvStatus.text = "待上门"
        // 已超时
        binding.includeTimeout.tvStatus.text = "已超时"
        // 上门中
        binding.includeDoorInIng.tvStatus.text = "待确认服务"
        // 服务中
        binding.includeServicing.tvStatus.text = "服务中"
        // 待验收
        binding.includeUnaccepted.tvStatus.text = "待验收"
        // 返工单-待上门
        binding.includeReturnUnDoorIn.tvStatus.text = "待上门"
        // 返工单-服务中
        binding.includeReturnServicing.tvStatus.text = "服务中"
        // 返工单-待验收
        binding.includeReturnUnaccepted.tvStatus.text = "待验收"
        // 返工单-已完成
        binding.includeReturnFinish.tvStatus.text = "已完成"

        when(status){
            1 -> {
                // 待服务
                // 待预约
                binding.includeAppointment.root.visible()
                // 待确认
                binding.includeUnconfirmed.root.visible()
                // 待上门
                binding.includeUnDoorIn.root.visible()
                // 已超时
                binding.includeTimeout.root.visible()
            }
            2 -> {
                // 进行中
                // 上门中
                binding.includeDoorInIng.root.visible()
                // 服务中
                binding.includeServicing.root.visible()
                // 待验收
                binding.includeUnaccepted.root.visible()
            }
            5 -> {
                // 返工单
                binding.includeReturnUnDoorIn.root.visible()
                binding.includeReturnServicing.root.visible()
                binding.includeReturnUnaccepted.root.visible()
                binding.includeReturnFinish.root.visible()
            }
        }

        chooseOrderStatus()

        // 待预约
        binding.includeAppointment.tvStatus.clickWithTrigger {
            if (orderStatus == 9){
                (fragments[0] as AppointmentOrderFragment).refreshData()
            }else {
                orderStatus = 9
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(0, false)
                (fragments[0] as AppointmentOrderFragment).refreshData()
            }
        }
        // 待确认
        binding.includeUnconfirmed.tvStatus.clickWithTrigger {
            if (orderStatus == 8){
                (fragments[1] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 8
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(1, false)
                (fragments[1] as OrderListStatusFragment).refreshData()
            }
        }
        // 待上门
        binding.includeUnDoorIn.tvStatus.clickWithTrigger {
            if (orderStatus == 1){
                (fragments[2] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 1
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(2, false)
                (fragments[2] as OrderListStatusFragment).refreshData()
            }
        }
        // 已超时
        binding.includeTimeout.tvStatus.clickWithTrigger {
            if (orderStatus == 7){
                (fragments[3] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 7
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(3, false)
                (fragments[3] as OrderListStatusFragment).refreshData()
            }
        }
        // 上门中
        binding.includeDoorInIng.tvStatus.clickWithTrigger {
            if (orderStatus == 10){
                (fragments[0] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 10
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(0, false)
                (fragments[0] as OrderListStatusFragment).refreshData()
            }
        }
        // 服务中
        binding.includeServicing.tvStatus.clickWithTrigger {
            if (orderStatus == 2){
                (fragments[1] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 2
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(1, false)
                (fragments[1] as OrderListStatusFragment).refreshData()
            }
        }
        // 待验收
        binding.includeUnaccepted.tvStatus.clickWithTrigger {
            if (orderStatus == 6){
                (fragments[2] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 6
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(2, false)
                (fragments[2] as OrderListStatusFragment).refreshData()
            }
        }
        // 返工单-待上门
        binding.includeReturnUnDoorIn.tvStatus.clickWithTrigger {
            if (orderStatus == 1){
                (fragments[0] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 1
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(0, false)
                (fragments[0] as OrderListStatusFragment).refreshData()
            }
        }
        // 返工单-服务中
        binding.includeReturnServicing.tvStatus.clickWithTrigger {
            if (orderStatus == 2){
                (fragments[1] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 2
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(1, false)
                (fragments[1] as OrderListStatusFragment).refreshData()
            }
        }
        // 返工单-待验收
        binding.includeReturnUnaccepted.tvStatus.clickWithTrigger {
            if (orderStatus == 6){
                (fragments[2] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 6
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(2, false)
                (fragments[2] as OrderListStatusFragment).refreshData()
            }
        }
        // 返工单-已完成
        binding.includeReturnFinish.tvStatus.clickWithTrigger {
            if (orderStatus == 3){
                (fragments[3] as OrderListStatusFragment).refreshData()
            }else {
                orderStatus = 3
                chooseOrderStatus()
                binding.viewPager.setCurrentItem(3, false)
                (fragments[3] as OrderListStatusFragment).refreshData()
            }
        }

        initViewPager()

    }


    private fun initViewPager() {
        fragments.clear()
        when(status){
            1 -> {
                // 待服务
                fragments.add(AppointmentOrderFragment.getInstance()) // 预约
                fragments.add(OrderListStatusFragment.getInstance(8))
                fragments.add(OrderListStatusFragment.getInstance(1))
                fragments.add(OrderListStatusFragment.getInstance(7))
            }
            2 -> {
                // 进行中
                fragments.add(OrderListStatusFragment.getInstance(10))
                fragments.add(OrderListStatusFragment.getInstance(2))
                fragments.add(OrderListStatusFragment.getInstance(6))
            }
            5 -> {
                // 返工单
                fragments.add(OrderListStatusFragment.getInstance(1, 1))
                fragments.add(OrderListStatusFragment.getInstance(2, 1))
                fragments.add(OrderListStatusFragment.getInstance(6, 1))
                fragments.add(OrderListStatusFragment.getInstance(3, 1))
            }
        }
        val adapter = ViewPagerAdapter(childFragmentManager, lifecycle, fragments)
        binding.viewPager.adapter = adapter
        binding.viewPager.isUserInputEnabled = false
        binding.viewPager.offscreenPageLimit = fragments.size
    }

    /**
     * 设置订单状态选中
     * 8: 待确认, 1: 待上门, 7: 已超时, 2: 服务中, 6: 待验收, 3: 已完成, 4: 退款/售后, 9: 待预约 10 上门中
     */
    private fun chooseOrderStatus(){
        // 待预约
        binding.includeAppointment.tvStatus.isSelected = orderStatus == 9 && status == 1
        // 待确认
        binding.includeUnconfirmed.tvStatus.isSelected = orderStatus == 8 && status == 1
        // 待上门
        binding.includeUnDoorIn.tvStatus.isSelected = orderStatus == 1 && status == 1
        // 已超时
        binding.includeTimeout.tvStatus.isSelected = orderStatus == 7 && status == 1
        // 上门中
        binding.includeDoorInIng.tvStatus.isSelected = orderStatus == 10 && status == 2
        // 服务中
        binding.includeServicing.tvStatus.isSelected = orderStatus == 2 && status == 2
        // 待验收
        binding.includeUnaccepted.tvStatus.isSelected = orderStatus == 6 && status == 2
        // 返工单-待上门
        binding.includeReturnUnDoorIn.tvStatus.isSelected = orderStatus == 1 && status == 5
        // 返工单-服务中
        binding.includeReturnServicing.tvStatus.isSelected = orderStatus == 2 && status == 5
        // 返工单-待验收
        binding.includeReturnUnaccepted.tvStatus.isSelected = orderStatus == 6 && status == 5
        // 返工单-已完成
        binding.includeReturnFinish.tvStatus.isSelected = orderStatus == 3 && status == 5
    }

    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    override fun startObserve() {

        // 订单状态切换事件
        // 9: 待预约, 8: 待确认, 1: 待上门, 7: 已超时, 2: 服务中, 6: 待验收, 3: 已完成, 5: 返工单, 4: 退款/售后 10 上门中
        LiveEventBus.get(OrderStatusChangeEvent::class.java).observeSticky(this){

            if (status == it.status){
                orderStatus = it.orderStatus ?:0
                chooseOrderStatus()
                when(it.orderStatus){
                    9,10 -> {
                        binding.viewPager.setCurrentItem(0, false)
                        (fragments[0] as OrderListStatusFragment).refreshData()
                    }
                    8,2 -> {
                        binding.viewPager.setCurrentItem(1, false)
                        (fragments[1] as OrderListStatusFragment).refreshData()
                    }
                    1,6 -> {
                        binding.viewPager.setCurrentItem(2, false)
                        (fragments[2] as OrderListStatusFragment).refreshData()
                    }
                    7 -> {
                        binding.viewPager.setCurrentItem(3, false)
                        (fragments[3] as OrderListStatusFragment).refreshData()
                    }
                }
            }


        }
    }

    /**
     * 获取当前的订单状态
     * @return Int
     */
    fun getOrderStatus(): Int{
        if (status == 5) return 5
        return orderStatus
    }

    /**
     * 设置当前订单数量
     * @param numBean NumBean?
     */
    fun setOrderNum(numBean: NumBean?){
        // 待预约
        binding.includeAppointment.tvStatusNum.visibleOrGone(numBean?.service_yuyue_count != 0)
        binding.includeAppointment.tvStatusNum.text = "${numBean?.service_yuyue_count}"
        // 待确认
        binding.includeUnconfirmed.tvStatusNum.visibleOrGone(numBean?.confirm_service_count != 0)
        binding.includeUnconfirmed.tvStatusNum.text = "${numBean?.confirm_service_count}"
        // 待上门
        binding.includeUnDoorIn.tvStatusNum.visibleOrGone(numBean?.wait_door_count != 0)
        binding.includeUnDoorIn.tvStatusNum.text = "${numBean?.wait_door_count}"
        // 已超时
        binding.includeTimeout.tvStatusNum.visibleOrGone(numBean?.timeout_door_count != 0)
        binding.includeTimeout.tvStatusNum.text = "${numBean?.timeout_door_count}"
        // 上门中
        binding.includeDoorInIng.tvStatusNum.visibleOrGone(numBean?.work_door_count != 0)
        binding.includeDoorInIng.tvStatusNum.text = "${numBean?.work_door_count}"
        // 服务中
        binding.includeServicing.tvStatusNum.visibleOrGone(numBean?.service_on_count != 0)
        binding.includeServicing.tvStatusNum.text = "${numBean?.service_on_count}"
        // 待验收
        binding.includeUnaccepted.tvStatusNum.visibleOrGone(numBean?.service_accept_count != 0)
        binding.includeUnaccepted.tvStatusNum.text = "${numBean?.service_accept_count}"
        // 返工单-待上门
        binding.includeReturnUnDoorIn.tvStatusNum.visibleOrGone(numBean?.rework_wait_door_count != 0)
        binding.includeReturnUnDoorIn.tvStatusNum.text = "${numBean?.rework_wait_door_count}"
        // 返工单-服务中
        binding.includeReturnServicing.tvStatusNum.visibleOrGone(numBean?.rework_service_on_count != 0)
        binding.includeReturnServicing.tvStatusNum.text = "${numBean?.rework_service_on_count}"
        // 返工单-待验收
        binding.includeReturnUnaccepted.tvStatusNum.visibleOrGone(numBean?.rework_service_accept_count != 0)
        binding.includeReturnUnaccepted.tvStatusNum.text = "${numBean?.rework_service_accept_count}"
        // 返工单-已完成
        binding.includeReturnFinish.tvStatusNum.visibleOrGone(numBean?.rework_finish_service_count != 0)
        binding.includeReturnFinish.tvStatusNum.text = "${numBean?.rework_finish_service_count}"

    }

}